import Vue from 'vue';
import VueRouter from 'vue-router';
// 引入组件
import Login from '../views/Login.vue';
import Index from '../views/Index';
import Home from '../views/Home.vue';
import Cart from '../components/index/Cart.vue';
import Photo from '../components/picture/Picture-sharing.vue';
import Detail from '../components/picture/Picture-detail.vue';
import Search from '../components/search/Search.vue';
import AddGoods from '../components/goods/Add.vue';
import GoodsDetail from '../components/goods/Detail.vue';
import NewsList from '../components/news/List.vue';
import NewsDetail from '../components/news/Detail.vue';
import GoodsList from '../components/goods/List.vue';
import Member from '../views/Member.vue';
import Orders from '../views/Order.vue';
import bdmap from '../views/bdmap.vue';
import GoodsClassify from '../views/Classify.vue';
import GoodsFilter from '../components/goods/Filter.vue';
import ErrorPage from '../views/error.vue';
import Filter from '../components/order/Filter.vue';
// 引入公共组件
import Comment from '../components/common/Comment.vue';
import Building from '../components/common/Building.vue';
Vue.component('comment', Comment);
Vue.component('building', Building);
Vue.component('order-filter', Filter);

const routes = [{ // 默认跳转
    path: '/',
    redirect: '/index'
  },
  {
    path: '/login',
    component: Login
  },
  { // 默认跳转首页
    path: '/index',
    redirect: '/home',
    component: Index,
    children: [{ // 首页
        path: '/home',
        component: Home
      }, { // 购物车
        path: '/cart',
        component: Cart
      },
      { // 图片分享页面
        path: '/sharing',
        component: Photo
      },
      { // 图片详情页面
        path: '/detail',
        component: Detail
      },
      { // 搜索页面
        path: '/search',
        component: Search
      },
      { // 添加商品页面
        path: '/addgoods',
        component: AddGoods
      },
      { // 商品详情页面
        path: '/goods/detail',
        component: GoodsDetail,
      },
      { // 文章列表页面
        path: '/news/list',
        component: NewsList
      },
      { // 文章详情页面
        path: '/news/detail',
        component: NewsDetail
      },
      {
        path: '/goods/list',
        component: GoodsList
      },
      {
        path: '/map',
        component: bdmap
      },
      {
        path: '/feedback',
        component: Building
      },
      {
        path: '/video',
        component: Building
      }
    ]
  },
  {
    path: '/member',
    component: Member
  },
  {
    path: '/goods/classify',
    component: GoodsClassify,
  },
  {
    path: '/goods/filter',
    component: GoodsFilter
  },
  {
    path: '/goods/filter',
    component: GoodsFilter
  },
  {
    path: '/orders',
    component: Orders
  },
  {
    path: "/404",
    component: ErrorPage
  },
  {
    path: "*",
    redirect: "/404",
    hidden: true
  }
]

const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push(location) {
  this.app.$store.commit(location === '/search' ? 'showAddGoodButton' : 'hideAddGoodButton');
  return originalPush.call(this, location).catch(err => err)
}

Vue.use(VueRouter);

const router = new VueRouter({
  routes
})

router.beforeEach((to, from, next) => {
  if (!/Android|webOS|iPhone|iPod|BlackBerry/i.test(navigator.userAgent)) {
    window.location.href = '/admin.html#/'
    return;
  }
  if (to.path === '/login') {
    return next();
  }
  const tokenString = window.sessionStorage.getItem('token');
  if (!tokenString) {
    return next('/login');
  }
  next();
});

export default router
